package cn.maxwell.org.service.impl;


import cn.maxwell.basic.service.impl.BaseServiceImpl;
import cn.maxwell.basic.util.Md5Utils;
import cn.maxwell.basic.util.StrUtils;
import cn.maxwell.org.domain.Employee;
import cn.maxwell.org.mapper.EmployeeMapper;
import cn.maxwell.org.service.IEmployeeService;
import cn.maxwell.user.domain.Logininfo;
import cn.maxwell.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 添加操作
     * @param employee
     */
    @Transactional
    @Override
    public void add(Employee employee) {
        //1.获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        //1.2设置盐值
        employee.setSalt(salt);
        //2.设置密码
        String password = employee.getPassword();
        String md5Password = Md5Utils.encrypByMd5(password + salt);
        employee.setPassword(md5Password);
        //employee.setPassword(Md5Utils.encrypByMd5(employee.getPassword())+salt);

        //3.添加logininfo登录信息
        Logininfo logininfo = employee2logininfo(employee);
        //3.1 添加logininfo登录信息到数据库
        logininfoMapper.add(logininfo);
        //4.employee表添加操作
        //4.1 获取logininfo自增主键
        Long logininfo_id = logininfo.getId();
        //4.2 将logininfo自增主键赋值给employee表logininfo——id字段
        employee.setLogininfo_id(logininfo_id);
        //调用添加方法将employee添加到数据库
        employeeMapper.add(employee);

        //如果角色不为空的话
        //添加中间表t_employee_role表信息（员工id，角色id）
        if(employee.getRole_id()!=null){
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }
    }

    private Logininfo employee2logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        //copy一份employee表的数据到logininfo
        BeanUtils.copyProperties(employee,logininfo);
        //设置type；当前对象是员工 1代表用户  0代表员工
        logininfo.setType(0);
        return logininfo;
    }

    /**
     * 修改操作
     * @param employee
     */
    @Transactional
    @Override
    public void update(Employee employee) {
        //1.盐值
        //1.1获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        //1.2设置盐值
        employee.setSalt(salt);

        //2.设置加密加盐的密码
        String password = employee.getPassword();
        String md5Password = Md5Utils.encrypByMd5(password + salt);
        employee.setPassword(md5Password);
        //employee.setPassword(Md5Utils.encrypByMd5(employee.getPassword())+salt);

        //3.修改logininfo登录信息
        //3.1 获取登录信息id（修改操作不改变自增主键）
        Long logininfo_id = employeeMapper.loadById(employee.getId()).getLogininfo_id();
        //3.2 获取修改数据（与employee表同步）
        //再次调用获取数据的方法
        Logininfo logininfo = employee2logininfo(employee);
        //设置id
        logininfo.setId(logininfo_id);
        //3.3 更新修改到logininfo表
        logininfoMapper.add(logininfo);

        //删除中间表信息
        employeeMapper.deleteEmployeeRole(employee.getId());
        //如果角色不为空的话
        //添加中间表t_employee_role表信息（员工id，角色id）
        if(employee.getRole_id()!=null){
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }

        //4.更新修改到employee表
        employeeMapper.update(employee);
    }

    /**
     * 删除操作
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id) {
        /*//1.获取要删除的登录信息logininfo表id
        Long logininfo_id = employeeMapper.loadById(id).getLogininfo_id();
        //1.1 调用删除方法删除logininfo
        logininfoMapper.delete(logininfo_id);*/


        //1.获取要删除的登录信息logininfo表id
        Employee employee = employeeMapper.loadById(id);
        //删除登录信息表数据
        logininfoMapper.delete(employee.getLogininfo_id());

        //删除中间表信息
        employeeMapper.deleteEmployeeRole(id);

        //2.删除employee表信息
        employeeMapper.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 根据店铺id查询employee
     * @param shop_id
     * @return
     */
    @Override
    public List<Employee> findByShopId(Long shop_id) {
        return employeeMapper.findShopEmployee(shop_id);
    }
}